<?php
class MY_Controller extends Controller
{
	private $resource = null; 
	private $priviledge = null;
	private $default_role = 'Guest';
	private $ci;
	private $acl;	
	private $language;
	
	public
		$layout = 'default',
		$view = 'index',
		$data = array(),
		$message = ''
	;
	
	function __construct()
	{
		parent::__construct();		

		$this->data['wysiwyg'] = false;

		// Defines the assets
		/*
		$this->assetlibpro->add_css('/layout/admin/css/reset.css');
		$this->assetlibpro->add_css('/layout/admin/css/main.css');
		$this->assetlibpro->add_css('/layout/admin/css/2col.css');
		$this->assetlibpro->add_css('/layout/admin/css/1col.css');
		$this->assetlibpro->add_css('/layout/admin/css/style.css');
		$this->assetlibpro->add_css('/layout/admin/css/trufflehead.css');		
		*/
		$this->assetlibpro->add_js('/js/jquery/jquery-1.3.2.js');
		//$this->assetlibpro->add_js('/js/jquery/jquery.tablesorter.min.js');
		$this->assetlibpro->add_js('/layout/admin/js/overall.js');
		/*$this->assetlibpro->add_js('/layout/admin/js/toggle.js');
		$this->assetlibpro->add_js('/layout/admin/js/ui.core.js');
		$this->assetlibpro->add_js('/layout/admin/js/ui.tabs.js');*/
		
		// Defines the language
		$lang = $this->uri->segment(1);
		
		// Loads nedeed language
		if (strlen($lang) == '2')
		{
			$this->language = $lang;
			$this->lang->load('str', $lang);
			$this->config->set_item('language', $lang);			
		}
		else 
		{
			// By default
			$this->language = 'en';
			$this->lang->load('str', 'en');
			$this->config->set_item('language', 'en');
		}
	    
		$this->acl = init_roles();		  
		$router =& load_class('Router');
		$this->resource   = $router->fetch_class(); 
		$this->priviledge = $router->fetch_method();   
		$this->ci =& get_instance();		
		$acl = $this->getACL();

		$id_user = $this->auth->getID();
		
		if(!empty($id_user))
		{
			$this->setDefaultRole($this->auth->getUserGroup());
		}

		if(!$acl->isAllowed($this->getDefaultRole(), $this->getResource(), $this->getPriviledge()))
		{
			redirect('admins/prohibited_page','refresh');
		}		
   
		// Defines layout
		$this->layout = 'admin';
		
		// Defines view
		$this->view = $this->router->fetch_class() . '/' . $this->router->fetch_method();

		// Checks single login
		if (!$this->auth->is_logged_in() && $this->view !== 'admins/login' && $this->router->method !== 'password_forgot' && $this->router->method !== 'reset_password' && $this->router->method !== 'activation' && $this->router->method !== 'search_autocomplete')
		{
			redirect('admins/login');
		}
	}
	
	function renderLayout($print = FALSE)
	{ 
		if ($print == FALSE)
		{
			$this->load->view('_layouts/' . $this->layout, $this->data, $print);			
		}
		else
		{
			return $this->load->view('_layouts/' . $this->layout, $this->data, $print);
		}		
	}

	//TODO: does not work in layout view - CI_loader
	function renderElement($element = null)
	{
		$this->load->view('_elements/' . $element, $this->data);
	}
	
   
   public function getInstance(){
      return $this->ci;
   }

   public function getACL(){
      return $this->acl;
   }

   public function getDefaultRole(){
      return $this->default_role;
   }

   public function getPriviledge(){
      return $this->priviledge;
   }
   public function getResource(){
      return $this->resource;
   }

   public function setDefaultRole($role){
     $this->default_role = $role;
   }

   public function getLang(){
      return $this->language;
   }	
   
}
?>